package com.ojoin.trade.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Config {

	private final static Logger logger = LoggerFactory
			.getLogger(Config.class);
	
	public final static String sysConfig="sysConfig";
	
	public final static String defConfig="resources";
	
	// 配置信息
	private static Map<String,Properties> confMap=new HashMap<String,Properties>();
	

	public static String getProperty(String key) {
		return getConfiguration(sysConfig).getProperty(key);
	}
	
	public static boolean containsKey(String key) {
		return getConfiguration(sysConfig).containsKey(key);
	}

	public static boolean containsKey(String fileName,String key) {
		return getConfiguration(fileName).containsKey(key);
	}

	public static String getProperty(String fileName,String key) {
		return getConfiguration(fileName).getProperty(key);
	}

	
	
	public static synchronized Properties getConfiguration(String fileKey) {
		if (null==confMap.get(fileKey)) {
			Properties configuration = new Properties();
			
			
			InputStream resourceAsStream = Config.class
					.getResourceAsStream("/"+fileKey+".properties");
			if (resourceAsStream != null) {
				try {
					configuration.load(new InputStreamReader(resourceAsStream, "UTF-8"));
					confMap.put(fileKey, configuration);
					logger.info("{}.properties 加载完成",sysConfig);
				} catch (IOException e) {
					e.printStackTrace();
					logger.error("{}.properties 加载异常",sysConfig);
				}
			}else{
				logger.error("{}.properties 加载异常",sysConfig);
			}
		}
		
		return confMap.get(fileKey);
	}

}
